import 'package:flutter/material.dart';
import 'package:item_detail_demo/item_detail/manager/item_detail_manager.dart';

import 'components/bottom_bar.dart';
import 'components/recommend_card.dart';
import 'manager/item_detail_data_manager.dart';
import 'model/recommend_card_bean.dart';
import 'package:provider/provider.dart';

const int kMockRecommendItemCount = 1000;

class ItemDetail extends StatefulWidget {
  const ItemDetail({Key? key}) : super(key: key);

  @override
  State<ItemDetail> createState() => _ItemDetailState();
}

class _ItemDetailState extends State<ItemDetail> {

  final ItemDetailDataManager dataManager = ItemDetailDataManager();

  Widget _buildTitleBar() {
    final Size screenSize = MediaQuery.of(context).size;
    return Row(
      children: [
        const SizedBox(
          width: 60,
        ),
        Container(
          width: screenSize.width - 150,
          alignment: Alignment.center,
          margin: const EdgeInsets.only(top: 2.0, right: 6.0),
          height: 32.0,
          decoration: BoxDecoration(
            color: const Color(0xFF8E8E93).withAlpha(20),
            borderRadius: BorderRadius.circular(16.0),
          ),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              const SizedBox(width: 10.0),
              Image.asset(
                'assets/images/search_icon.webp',
                width: 20.0,
                height: 20.0,
              ),
              const SizedBox(width: 2.0),
              const Expanded(
                child: Text(
                  '搜索你要的宝贝',
                  style: TextStyle(
                    fontSize: 14.0,
                    color: Color(0xFFB3B3B3),
                  ),
                ),
              )
            ],
          ),
        ),
        Container(
          margin: const EdgeInsets.only(left: 16, right: 16),
          child: Image.asset(
            'assets/images/share.png',
            width: 20.0,
            height: 20.0,
          ),
        ),
        Image.asset(
          'assets/images/more.png',
          width: 20.0,
          height: 20.0,
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Provider<ItemDetailDataManager>(
        create: (BuildContext context) {
          return dataManager;
        },
        child: SafeArea(
            child: Stack(
              children: [
                CustomScrollView(
                  slivers: [
                    SliverAppBar(
                      pinned: true,
                      backgroundColor: const Color(0xFFFAFAFA),
                      foregroundColor: const Color(0xFF000000),
                      // expandedHeight: 250.0,
                      flexibleSpace: FlexibleSpaceBar(
                        centerTitle: true,
                        title: _buildTitleBar(),
                      ),
                    ),
                    SliverList(delegate: SliverChildBuilderDelegate(
                          (BuildContext context, int index) {
                        return ItemDetailManager.instance.buildComponent(dataManager.components[index]);
                      },
                      childCount: dataManager.components.length,
                    ),),
                    SliverPadding(
                      padding: const EdgeInsets.all(8.0),
                      sliver: SliverGrid(
                        //Grid
                        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                          crossAxisCount: 2,
                          mainAxisSpacing: 8.0,
                          crossAxisSpacing: 8.0,
                          childAspectRatio: 0.64,
                        ),
                        delegate: SliverChildBuilderDelegate(
                              (BuildContext context, int index) {
                            final int aIndex = index % dataManager.recommendData.length;
                            final Map itemData = dataManager.recommendData[aIndex];
                            return RecommendCard(PItemCommonRecommendCard(itemData));
                          },
                          childCount: kMockRecommendItemCount,
                        ),
                      ),
                    ),
                    SliverList(delegate: SliverChildBuilderDelegate(
                          (BuildContext context, int index) {
                        return Container(
                          height: 85,
                          alignment: Alignment.topCenter,
                          child: const Text('已经到底了',
                            style: TextStyle(
                              fontSize: 14.0,
                              color: Color(0xFFB3B3B3),
                            ),),
                        );
                      },
                      childCount: 1,
                    ),),
                  ],
                ),
                const Positioned(bottom: 0,child: BottomBar(),),
              ],
            )
        ),
      )
    );
  }
}
